package com.training.admin.web.controller.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.MenuSetting;
import com.training.admin.entity.department.Department;
import com.training.admin.entity.principal.Principal;
import com.training.admin.entity.security.Login;
import com.training.admin.service.DepartmentService;
import com.training.admin.service.PrincipalService;

/**
 * 
 * @author Rockett
 * 
 */
@Controller
@RequestMapping("/profile")
@MenuSetting("menu-system-management")
public class ProfileController {
 
    @Autowired
    private PrincipalService principalService;
    @Autowired
    private DepartmentService departmentService;

    /**
     * 
     * @return
     */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@MenuSetting("submenu-profile-management")
    public ModelAndView profile() {
		ModelAndView modelAndView = new ModelAndView("account/profile");
		Login login = (Login)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		//Principal principal = principalService.load(Principal.class, login.getId());
		Principal principal = principalService.loadByEmail(login.getEmail());
		modelAndView.addObject("currentAccount", principal);
		if(principal.getAuthority().equals("ROLE_DEPARTMENT_USER")){
			Department department = departmentService.load(Department.class, principal.getDepartmentId());
			modelAndView.addObject("departmentName", department.getName());
		}
		return modelAndView;
    }
    

	
}
